package com.lc.ibps.platform.rest.org;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.org.service.IPartyOrgMgrService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/** 组织控制器，第三方系统调用
 *
 * <pre> 
 * 构建组：ibps-platform-webapi
 * 作者：Lium
 * 邮箱：1316679699@qq.com
 * 日期：2017年7月19日-下午5:50:26
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */

@RequestMapping(value="/api/webapi/orgMgrService")
@Api(value = "/orgMgrService",tags="用户管理-组织管理服务")
@RestController
public class OrgMgrControlle extends BaseApiController{
	
	
	private IPartyOrgMgrService orgMgrService;
	
	public OrgMgrControlle(){
		orgMgrService = AppUtil.getBean(IPartyOrgMgrService.class);
	}
	
	/**
	 * 添加组织信息
	 *
	 * @param json {id:""/*选填/,name:"开发部"/*必填/,orgAlias:"kfb"/*必填/,
	 * 				levelID:""/*选填/,status:""/*选填/,partyEntityPo.parentId:""/*选填/,
	 * 				createTime:""/*选填/}
	 */
	@ApiOperation(value = "添加组织信息", notes = "添加组织信息")
	@RequestMapping(value="/createOrg",method=RequestMethod.PUT)
	public APIResult<String> createOrg(
			@ApiParam(value = "组织信息json", required = true)
			@RequestParam(name = "json", required = true) String json
			){
		APIResult<String> result = new APIResult<>();
		try{
			orgMgrService.createOrg(json);			//TODO 异常错误
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "添加组织信息失败！", e);
		}
		return result;
	}
	
	/**
	 * 修改组织信息
	 *
	 * @param json {id:"e4f00720"/*必填/,name:"开发部"/*必填/,orgAlias:"kfb"/*必填/,
	 * 				levelID:""/*选填/,status:""/*选填/,partyEntityPo.parentId:""/*选填/,
	 * 				createTime:"2017-01-20 12:33:06"/*必填/}
	 */
	@ApiOperation(value = "修改组织信息", notes = "修改组织信息")
	@RequestMapping(value="/updateOrg",method=RequestMethod.PUT)
	public APIResult<String> updateOrg(
			@ApiParam(value = "组织信息json", required = true)
			@RequestParam(name = "json", required = true) String json
			){
		APIResult<String> result = new APIResult<>();
		try{
			orgMgrService.updateOrg(json);			//TODO 异常错误
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "修改组织信息失败！", e);
		}
		return result;
	}
	
	@ApiOperation(value = "删除组织信息", notes = "根据组织ID删除组织信息")
	@RequestMapping(value="/deleteOrg",method=RequestMethod.DELETE)
	public APIResult<String> deleteOrg(
			@ApiParam(value = "组织ID", required = true)
			@RequestParam(name = "id", required = true) String id
			) {	
		APIResult<String> result = new APIResult<>();
		try{
			orgMgrService.deleteOrg(id);			
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "删除组织信息失败！", e);
		}
		return result;
	}
	

	@ApiOperation(value = "人员加入组织", notes = "根据组织ID添加用户")
	@RequestMapping(value="/addEmployee2Org",method=RequestMethod.PUT)
	public APIResult<String> addEmployee2Org(
			@ApiParam(value = "组织ID", required = true) 
			@RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "用户ID", required = true) 
			@RequestParam(name = "userId", required = true) String userId
			) {	
		APIResult<String> result = new APIResult<>();
		try{
			orgMgrService.addEmployee2Org(id, userId);			
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "人员加入组织失败！", e);
		}
		return result;
	}
	
	@ApiOperation(value = "角色加入组织", notes = "根据组织ID添加角色")
	@RequestMapping(value="/addRole2Org",method=RequestMethod.PUT)
	public APIResult<String> addRole2Org(
			@ApiParam(value = "组织ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "角色ID", required = true) @RequestParam(name = "roleId", required = true) String roleId) {
		APIResult<String> result = new APIResult<>();
		try {
			orgMgrService.addRole2Org(id, roleId);	
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "角色加入组织失败！", e);
		}
		return result;
	}
	
	@ApiOperation(value = "岗位加入组织", notes = "根据组织ID添加岗位")
	@RequestMapping(value="/addPosition2Org",method=RequestMethod.PUT)
	public APIResult<String> addPosition2Org(
			@ApiParam(value = "组织ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "positions", required = true) String positions) {
		APIResult<String> result = new APIResult<>();
		try{
			orgMgrService.addPosition2Org(id, positions);			
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "岗位加入组织失败！", e);
		}
		return result;
	}
	
	@ApiOperation(value = "添加组织负责人", notes = "根据组织ID和用户ID添加负责人")
	@RequestMapping(value="/addManager4Org",method=RequestMethod.PUT)
	public APIResult<String> addManager4Org(
			@ApiParam(value = "组织ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId) {
		APIResult<String> result = new APIResult<>();
		try{
			orgMgrService.addManager4Org(id, userId);			
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "添加组织负责人失败！", e);
		}
		return result;	
	}
	
	@ApiOperation(value = "组织移除人员", notes = "根据组织Id移除用户")
	@RequestMapping(value="/removeEmployee4Org",method=RequestMethod.DELETE)
	public APIResult<String> removeEmployee4Org(
			@ApiParam(value = "组织ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId) {
		APIResult<String> result = new APIResult<>();
		try{
			orgMgrService.removeEmployee4Org(id, userId);		
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "组织移除人员失败！", e);
		}
		return result;
	}
	
	@ApiOperation(value = "组织移除角色", notes = "根据组织ID移除角色")
	@RequestMapping(value="/removeRole4Org",method=RequestMethod.DELETE)
	public APIResult<String> removeRole4Org(
			@ApiParam(value = "组织ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "角色ID", required = true) @RequestParam(name = "roleId", required = true) String roleId) {
		APIResult<String> result = new APIResult<>();
		try{
			orgMgrService.removeRole4Org(id, roleId);	
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "组织移除角色失败！", e);
		}
		return result;
	}
	
	@ApiOperation(value = "组织移除岗位", notes = "根据用岗位ID移除岗位")
	@RequestMapping(value="/removePosition4Org",method=RequestMethod.DELETE)
	public APIResult<String> removePosition4Org(
		@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "positions", required = true) String positions) {
		APIResult<String> result = new APIResult<>();
		try{
			orgMgrService.removePosition4Org(positions); //TODO 没有报错但是没有移除
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "组织移除岗位失败！", e);
		}
		return result;
	}
	
	@ApiOperation(value = "移除组织负责人", notes = "根据组织ID和用户Id移除负责人")
	@RequestMapping(value="/removeManager4Org",method=RequestMethod.DELETE)
	public APIResult<String> removeManager4Org(
			@ApiParam(value = "组织ID", required = true) @RequestParam(name = "id", required = true) String id,
			@ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) String userId) {
		APIResult<String> result = new APIResult<>();
		try{
			orgMgrService.removeManager4Org(id, userId);
		}catch(Exception e){
			result = getExceptionResult(result, ERROR, "移除组织负责人失败！", e);
		}
		return result;
	}
}
